<template>
  <div class="flex flex-col h-full">
    <a-card :body-style="{ padding: '8px 12px' }">
      <a-form ref="formRef" :model="formState" class="flex justify-between">
        <div class="flex">
          <a-form-item name="brName" class="w-[260px] mr-3 !mb-0">
            <SelectTable
              v-model:value="formState.brName"
              :search-params="{ stdate: formState.dateRange?.[0], eddate: formState.dateRange?.[1] }"
              type="视光挂号患者列表退费"
              placeholder="姓名/助记码/卡号/手机号/身份证号"
              @select="onSelect"
            />
          </a-form-item>
          <a-form-item label="挂号时间" name="dateRange" class="!mb-0">
            <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
          </a-form-item>
        </div>

        <div>
          <a-button style="margin: 0 8px" @click="onClickReset">重置</a-button>
          <a-button type="primary" @click="chargeDetailList">刷新</a-button>
        </div>
      </a-form>
    </a-card>
    <PatientInfoHeader :patient-info="patientInfo" :show-insurance="true" class="mt-2" @change="onClickReset">
      <a-divider type="vertical" :style="{ height: '40px', margin: '0 16px' }" />
      <div>
        <div class="flex items-center">
          <LabelValue label="卡号" :value="patientInfo.brId" :width="160" label-class="w-[52px] text-right" />
          <LabelValue label="科室" :value="patientInfo.ksmc" :width="160" />
          <LabelValue label="类别" :value="patientInfo.ghlb" :width="160" />
        </div>
        <div class="flex items-center">
          <LabelValue label="门诊号" :value="patientInfo.jzbh" :width="160" />
          <LabelValue label="医生" :value="patientInfo.empName" :width="160" />
          <LabelValue label="时间" :value="patientInfo.ghsj" :width="200" />
        </div>
      </div>
      <a-divider type="vertical" :style="{ height: '40px', margin: '0 16px' }" />
      <div class="ml-10">
        <div class="flex items-center">
          <LabelValue label="原票据号" :value="patientInfo.pjhm" :width="160" />
          <LabelValue v-if="patientInfo.zfmx" :width="400" :tooltip="patientInfo.zfmx" :value="patientInfo.zfmx" />
          <LabelValue v-else label="支付" :value="'￥' + '0.00'" :width="160" />
        </div>
        <div class="flex items-center">
          <LabelValue label="原收费" :value="'￥' + patientInfo.zfy ? '￥' + formatAmount(patientInfo.zfy) : ''" :width="160" />
          <LabelValue label="时间" :value="patientInfo.sfsj" :width="220" />
        </div>
      </div>
    </PatientInfoHeader>
    <a-card class="mt-2">
      <div class="flex items-center justify-between mb-2">
        <BaseTitle title="费用明细" />
        <div class="flex items-center">
          <div class="text-red-500 w-52">
            本次应退：<span class="text-xl font-bold">￥{{ formatAmount(patientInfo.bcyt) }}</span>
          </div>
          <a-button type="primary" :disabled="tableDataDetail.length <= 0" @click="onClickCharge">退费</a-button>
        </div>
      </div>
      <BaseTable :loading="loading" column-code="00001119" fill :data-source="tableDataDetail" bordered>
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg, cj: record?.cj }" />
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Refund v-model:open="priceOpen" :register-price="patientInfo.bcyt" :vip-price="patientInfo.vipzf" :bill-number="billNumber" @success="onSuccessPrice" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import BaseTitle from '@/components/BaseTitle'
import SelectTable from '@/components/SelectTable'
import { formatAmount } from '@/utils/format.js'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import Refund from '../components/RefundModal.vue'
// import PatientInfo from '../components/PatientInfo.vue'

const loading = ref(false)
const tableDataDetail = ref([])
const formState = ref({
  dateRange: [dayjs().subtract(6, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
// const activeKey = ref('自费')
const priceOpen = ref(false)
const patientInfo = ref({})
const billNumber = ref('')
const keycode = ref('')
const formRef = ref(null)
const serialList = ref([])
const onClickReset = () => {
  formRef.value.resetFields()
  patientInfo.value = {}
  billNumber.value = ''
  keycode.value = ''
  tableDataDetail.value = []
}

const onSelect = async (select) => {
  formState.value.brName = select.brName
  formState.value.jzbh = select.jzbh
  billNumber.value = select.pjhm
  keycode.value = select.keycode
  getOutpatientInfo()
  chargeDetailList()
}

// 计算分币误差
const getReceiptsAmount = async () => {
  const { code, data, msg } = await post('/outp/oupCharge/getPennyDiscount', {
    ysje: Number(patientInfo.value.bcyt), //应退金额
    yhje: 0 //优惠金额
  })
  if (code !== 0) return message.error(msg)
  patientInfo.value.bcyt = data.ssje
  patientInfo.value.fbyh = data.fbwc
}

const onSuccessPrice = async (params) => {
  const { code, msg, data } = await post('/outp/oupCharge/doOutPRefundSg', { ...params, lshs: serialList.value, type: '0', fbyh: patientInfo.value.fbyh })
  if (code !== 0) return message.error(msg)
  priceOpen.value = false
  message.success(msg)
  await onPrint(data)
  onClickReset()
  // chargeDetailList()
}

const onPrint = async (row = {}) => {
  if (row.jslxbz === 1) {
    //挂号收费
    const { code, msg, data } = await post('/outp/outPrint/getRegistrationBillPrint', { jzbh: row.keycode, pjhm: row.pjhm, sftfbs: 1 })
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
  } else {
    //门诊收费
    const { code, msg, data } = await post('/outp/oupCharge/PrintMedicareCharges', { jzbh: row.keycode, pjhm: row.pjhm, sftfbs: 1 })
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
  }
}

//获取门诊患者信息
const getOutpatientInfo = async () => {
  const { data, code } = await post('/outp/mzys/getOutpRecordInfoSg', { mzjlbh: keycode.value, pjhm: billNumber.value })
  if (code === 0) {
    patientInfo.value = data
    getReceiptsAmount()
  }
}
const chargeDetailList = async () => {
  const { code, data, msg } = await post('/outp/oupCharge/getOutpRefundFeeListSg', { pjhm: billNumber.value })
  if (code !== 0) return message.error(msg)
  tableDataDetail.value = data
  data.forEach((item) => {
    serialList.value.push(item.dtlsh)
  })
}

const onClickCharge = () => {
  priceOpen.value = true
}
</script>
<style lang="less" scoped>
:deep(.ant-tabs-content-holder) {
  overflow: hidden;
}

.w60 {
  width: 60px;
}
</style>
